/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.core.resources.LocalFile;
import net.sourceforge.perforce.core.resources.LocalFolder;
import net.sourceforge.perforce.core.resources.LocalResource;

import org.eclipse.compare.internal.BufferedResourceNode;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.TeamException;

import java.util.List;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class PerforceResourceNode extends BufferedResourceNode {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** List that holds the children (if applicable). */
  private List<IStructureComparator> children;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for PerforceResourceNode.
   * 
   * @param resource the eclipse resource.
   */
  public PerforceResourceNode(IResource resource) {
    super(resource);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void setContent(byte[] contents) {
    try {
      super.setContent(contents);
      super.commit(null);
    } catch (CoreException e) {
      PerforceUIPlugin.log(e.getStatus());
    }
  }

  @Override
  public Object[] getChildren() {
    if (children == null) {
      children = Lists.newArrayList();

      IResource resource = getResource();
      if (resource instanceof IContainer) {
        try {
          LocalFolder localFolder = (LocalFolder) LocalResource.createFromResource(resource);

          LocalFile[] files = localFolder.getFiles();
          for (int i = 0, length = files.length; i < length; i++) {
            IResource child = files[i].toResource();
            if (child.exists()) {
              IStructureComparator childNode = createChild(child);
              if (childNode != null) {
                children.add(childNode);
              }
            }
          }

          LocalFolder[] folders = localFolder.getFolders();
          for (int i = 0, length = folders.length; i < length; i++) {
            IResource child = folders[i].toResource();
            if (child.exists()) {
              IStructureComparator childNode = createChild(child);
              if (childNode != null) {
                children.add(childNode);
              }
            }
          }
        } catch (TeamException e) {
          PerforceUIPlugin.log(e.getStatus());
        }
      }
    }

    return children.toArray();
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  /**
   * Creates a new child node for the given resource.
   * 
   * @param child the resource.
   * @return the new resource node.
   */
  @Override
  protected IStructureComparator createChild(IResource child) {
    return new PerforceResourceNode(child);
  }
}
